﻿using System;
using System.Collections.Generic;
using BSpec.Conventions;

namespace BSpec.Specification
{
    public class Context : SpecificationElement
    {
        private readonly List<Behaviour> _behaviours = new List<Behaviour>();

        protected Context(string name) 
            : base(name)
        {
        }

        public Behaviour[] Behaviours
        {
            get { return _behaviours.ToArray(); }
        }

        public static Context Create(Type testFixtureType)
        {
            string name = testFixtureType.GetName(NamingConvention.Context);

            return name.IsEmpty() ? new NullContext() : new Context(name);
        }

        public void AddBehaviourFor(Type testFixtureType)
        {
            var behaviour = Behaviour.Create(testFixtureType);

            if(_behaviours.HasElement(behaviour))
            {
                _behaviours.GetElement(behaviour).AddObservationsFor(testFixtureType);             
            }
            else
            {
                behaviour.AddObservationsFor(testFixtureType);
                _behaviours.Add(behaviour);
            }
        }
    }

    public class NullContext : Context
    {
        public NullContext()
            : base(string.Empty)
        {
        }
    }
}